package com.bjpowernode.controller;

import com.bjpowernode.vo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName MyController
 * @Description 当@RequestMapping中有公共的部分时，可以将公共部分放到类的上边
 * @Author hhwu
 * @Date 2022/3/10 13:04
 * Version 1.0
 **/

@Controller//创建处理器对象，和@Service,@Component一样
@RequestMapping("/user")//当方法上的@RequestMapping中有公共的部分时，可以将公共部分抽取放到类的上边，更加方便易辨别
public class MyController {

    @RequestMapping(value = {"/receiveproperty.do"})//不指定请求方式时没有限制
    public ModelAndView doThird(String name, Integer age) {
        /**
         * @Author hhwu
         * @Description //TODO 使用形参名和请求参数名一样的方式接收请求参数
         * @Date 9:22 2022/3/11
         * @Param [name, age]
         * @return org.springframework.web.servlet.ModelAndView
         **/
        ModelAndView mv = new ModelAndView();
        mv.addObject("myname", name);//用post传过来的中文会乱码，get方式不会，用过滤器可以解决post乱码问题
        mv.addObject("myage", age);
        mv.setViewName("show");
        return mv;
    }

    @RequestMapping(value = {"/receiveparam.do"})//不指定请求方式时没有限制
    public ModelAndView doReceiveParam(@RequestParam(value = "cname", required = false) String name, @RequestParam(value = "cage",required = false) Integer age) {
        /**
         * @Author hhwu
         * @Description //TODO 使用@RequestParam注解的方式映射请求参数和形参，来接收请求参数
         * @Date 9:23 2022/3/11
         * @Param [name, age]
         * @return org.springframework.web.servlet.ModelAndView
         **/
        ModelAndView mv = new ModelAndView();
        mv.addObject("myname", name);
        mv.addObject("myage", age);
        mv.setViewName("show");
        return mv;
    }

    @RequestMapping(value = {"/receiveobject.do"})//不指定请求方式时没有限制
    public ModelAndView doReceiveObject(Student student) {
        /**
         * @Author hhwu
         * @Description //TODO 使用java对象接收请求参数,student对象会自动接收请求参数，并调用set方法进行赋值
         * @Date 9:23 2022/3/11
         * @Param [name, age]
         * @return org.springframework.web.servlet.ModelAndView
         **/
        ModelAndView mv = new ModelAndView();
        mv.addObject("myname", student.getName());
        mv.addObject("myage", student.getAge());
        mv.setViewName("show");
        return mv;
    }
}
